package com.example.deliveryapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class CheckOutPageActivity extends AppCompatActivity {

    private EditText remarksEditText;
    private TextView orderTotalTextView;
    private RecyclerView orderItemsRecyclerView;
    private OrderItemsAdapter orderItemsAdapter;
    private List<Menu> menus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_out_page);

        // 初始化 UI 组件
        ImageButton backButton = findViewById(R.id.back_button);
        remarksEditText = findViewById(R.id.remarks_edit_text);
        orderTotalTextView = findViewById(R.id.order_total_text_view);
        orderItemsRecyclerView = findViewById(R.id.order_items_recycler_view);
        Button payButton = findViewById(R.id.pay_button);

        // 从 Intent 获取数据
        double totalPrice = getIntent().getDoubleExtra("total_price", 0);
        menus = getIntent().getParcelableArrayListExtra("order_items");

        // 筛选出已选择的菜品
        List<Menu> selectedMenus = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getSelectedQuantity() > 0) {
                selectedMenus.add(menu);
            }
        }

        // 初始化适配器并设置 RecyclerView
        orderItemsAdapter = new OrderItemsAdapter(selectedMenus);
        orderItemsRecyclerView.setAdapter(orderItemsAdapter);
        orderItemsRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 设置订单总金额
        orderTotalTextView.setText("Total: $" + String.format("%.2f", totalPrice));

        // 设置返回按钮
        backButton.setOnClickListener(v -> finish());

        // 设置付款按钮
        payButton.setOnClickListener(v -> {
            Intent selectPaymentIntent = new Intent(CheckOutPageActivity.this, SelectPaymentActivity.class);
            selectPaymentIntent.putExtra("total_price", totalPrice);
            selectPaymentIntent.putExtra("remarks", remarksEditText.getText().toString());
            startActivity(selectPaymentIntent);
        });
    }

    private class OrderItemsAdapter extends RecyclerView.Adapter<OrderItemsAdapter.OrderItemViewHolder> {
        private List<Menu> selectedMenus;

        public OrderItemsAdapter(List<Menu> menus) {
            this.selectedMenus = menus;
        }

        @NonNull
        @Override
        public OrderItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checkout_menu, parent, false);
            return new OrderItemViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull OrderItemViewHolder holder, int position) {
            Menu menu = selectedMenus.get(position);
            holder.itemName.setText(menu.getName());
            holder.itemImage.setImageResource(menu.getImageResourceId());
            holder.itemQuantity.setText(String.valueOf(menu.getSelectedQuantity()));
            holder.itemPrice.setText(String.format("$%.2f", menu.getPrice()));
        }

        @Override
        public int getItemCount() {
            return selectedMenus.size();
        }

        public class OrderItemViewHolder extends RecyclerView.ViewHolder {
            TextView itemName;
            ImageView itemImage;
            TextView itemQuantity;
            TextView itemPrice;

            public OrderItemViewHolder(@NonNull View itemView) {
                super(itemView);
                itemName = itemView.findViewById(R.id.item_name);
                itemImage = itemView.findViewById(R.id.item_image);
                itemQuantity = itemView.findViewById(R.id.item_quantity);
                itemPrice = itemView.findViewById(R.id.item_price);
            }
        }
    }
}